package com.hmsproject.search.managedbean;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.hmsproject.datastructures.deal.Criterion;
import com.hmsproject.datastructures.deal.Deal;
import com.hmsproject.datastructures.deal.DealDetails;
import com.hmsproject.db.HMSDBConnectionFactory;
import com.hmsproject.db.inmemorydb.DealMap;
import com.hmsproject.livedata.LiveDealCriterion;
import com.hmsproject.livedata.LiveDealData;

@ManagedBean
@SessionScoped
public class DealView {

	private String dealId;
	
	private boolean buyButtonEnabled;
	
	private LiveDealData dealData;
	
	private boolean offer;
	
	private int totalOffer;
	
	private int noOfRooms;

	public String getDealId() {
		return dealId;
	}
	
	public void setDealId(String dealId) {
		this.dealId = dealId;
	}
	
	public LiveDealData getDealData() {
		return dealData;
	}

	public void setDealData(LiveDealData dealData) {
		this.dealData = dealData;
	}
	
	public boolean isBuyButtonEnabled() {
		return buyButtonEnabled;
	}

	public void setBuyButtonEnabled(boolean buyButtonEnabled) {
		this.buyButtonEnabled = buyButtonEnabled;
	}
	
	public boolean isOffer() {
		return offer;
	}

	public void setOffer(boolean offer) {
		this.offer = offer;
	}	

	public int getTotalOffer() {
		return totalOffer;
	}

	public void setTotalOffer(int totalOffer) {
		this.totalOffer = totalOffer;
	}

	public void init(){
		
		System.out.println("Deal Id :"+this.dealId);
			//	this.dealData = HMSDBConnectionFactory.getInstance().getDBConnection().getLiveDealData(DealMap.getInstance().getDealForHotel(hotelId).getDealId());
		
		if(this.dealId != null){
			this.dealData = HMSDBConnectionFactory.getInstance().getDBConnection().getLiveDealData(this.dealId);
			this.setButtonEnabled();
		}
	}
	
	public void preRenderedInit(){
		 if (!FacesContext.getCurrentInstance().isPostback()) {
		        
			 System.out.println("Deal Id :"+this.dealId);
			 //	this.dealData = HMSDBConnectionFactory.getInstance().getDBConnection().getLiveDealData(DealMap.getInstance().getDealForHotel(hotelId).getDealId());
	
			 if(this.dealId != null){
				 this.dealData = HMSDBConnectionFactory.getInstance().getDBConnection().getLiveDealData(this.dealId);
                 this.setButtonEnabled();
			 }
		 }
	}
	
	
	private List<Integer> availableRooms;
	
	public List<Integer> getAvailableRooms(){
		if(this.availableRooms == null){
			availableRooms = new ArrayList<>();
            for (int i = 0; i <= this.dealData.getAvailableForBooking(); i++) {
				this.availableRooms.add(i);
			}
			
		}
		return this.availableRooms;
	}	
	
   public String getLiveDealCriterionsinHTML(){
		
		String dealCriterionStr = "";
		List<LiveDealCriterion> liveDealCriterions = dealData.getLiveDealCriterions();
		
		for(int i = 0; i < liveDealCriterions.size(); i++){
			LiveDealCriterion tmpLiveDealCriterion = liveDealCriterions.get(i);
			dealCriterionStr += "<p><b>"+tmpLiveDealCriterion.getNoOfRoomsMore()+"</b> more to get <b>"+tmpLiveDealCriterion.getAdditionalPercentOff()+"% off</b></p>"; 
		}
		if(dealCriterionStr == ""){
			offer = false;
			totalOffer = 0;
			Deal deal = DealMap.INSTANCE.getDeal(dealId);
			DealDetails dealDetails = deal.getDealDetails();
			List<Criterion> dealCriterions = dealDetails.getCriterions();
			for(Criterion dealCriterion:dealCriterions){
				 totalOffer += dealCriterion.getAdditionalPercentOff();
			}
		}else{
			offer = true;
		}
		return dealCriterionStr;
	}
   
   
   public String addToWatchList(){
	   return null;
   }
   
   
   private void setButtonEnabled(){

		  if(this.getDealData().getNoOfRooms() == 0){
			  this.setBuyButtonEnabled(false);
		  }else{
			  this.setBuyButtonEnabled(true);
		  }
   }
   
   public String fireNoOfRoomChangedAction(){
	  this.dealData = this.dealData.fireNoOfRoomChanged();
      this.setButtonEnabled();
	  return null;
   }
   
  
}
